package com.paimeng.Internetworm;

/**
 * @author: 潘辉
 * @Title: HtmlContents
 * @ProjectName: paimeng
 * @Description: 使用jsoup爬虫依据url获取需要的内容
 * @date: 2021/6/8 15:23
 */

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


import com.paimeng.bean.News_info;
import com.paimeng.utils.DBStatement;
import com.paimeng.utils.Times;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HtmlContents {

    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 15, 200, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5));
        long start = System.currentTimeMillis();
        System.out.println("activeCountMain1 : " + Thread.activeCount());
        for (int i = 0; i < 20; i++) {
            int finalI = i;
            executor.execute(new Runnable(){
                @Override
                public void run() {
                    readMySQL(finalI);
                }

            });
        }

        System.out.println("线程池中线程数目：" + executor.getPoolSize() + "，队列中等待执行的任务数目：" + executor.getQueue().size()
                + "，已执行玩别的任务数目：" + executor.getCompletedTaskCount());

        executor.shutdown();
        while (true) {
            if (executor.getActiveCount() == 0) {
                break;
            }
        }
        System.out.println("activeCountMain2 : " + Thread.activeCount());
        long end = System.currentTimeMillis();
        System.out.println("平均每秒可输出: " + 100000 / (end - start) + " 条");
    }

    public static void readMySQL(int i) {
        try {
            String sql = "select * from newsurl limit "+i*50+",50";
            Connection con = DBStatement.getDbConnection();
            assert con != null;
            PreparedStatement ps = con.prepareStatement(sql);
            ResultSet resultset = ps.executeQuery(sql);
            while (resultset.next()) {
                String url = resultset.getString(2);
                String title = resultset.getString(3);
                int hotindex = resultset.getInt(4);
                //将新闻内容存入磁盘的txt文件
                //File file = new File("text/" + UUID.randomUUID() + ".txt");

                Document document = Jsoup.parse(new URL(url), 30000);

                Elements sfel = document.select("a.up-name");
                String srcfrom = sfel.text();

                Elements els = document.select("div.read-article-holder > p");
                String content = "";
                for (Element el : els) {
                    if (content.length() < 100) {
                        content += el.getElementsByTag("p").eq(0).text();
                    } else {
                        content = content.substring(0, 100);
                        break;
                    }
                }
                News_info news_info = new News_info();
                news_info.setTitle(title);
                news_info.setUrl(url);
                news_info.setSrcFrom(srcfrom);
                news_info.setContent(content);
                news_info.setDate(Times.randomDate());
                news_info.setHotIndex(hotindex);
                news_info.setComment("null");
                NewsDAO.insertNewsInfo(news_info);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
